package org.example.demoarticle.Service.impl;


import com.github.pagehelper.PageHelper;
import org.example.demoarticle.Mapper.ArticleMapper;
import org.example.demoarticle.Service.ArticleService;
import org.example.demoarticle.pojo.Article;
import org.example.demoarticle.pojo.ArticleInsert;
import org.example.demoarticle.pojo.PageBean;
import org.example.demoarticle.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    ArticleMapper articleMapper;
    @Override
    public PageBean pageList(String username, String usertype, Integer current, Integer pageSize, String className, String creator, String category, String stem, LocalDate fromDate, LocalDate toDate) {
        PageHelper.startPage(current,pageSize);
        if(usertype.equals("tch")) {
            List<Article> list = articleMapper.tchList(username,usertype,className,creator,category,stem,fromDate,toDate);
            PageBean pageBean=new PageBean(list.size(),list);
            return pageBean;
        }else {
            List<Article> list = articleMapper.stuList(username,usertype,className,creator,category,stem,fromDate,toDate);
            PageBean pageBean=new PageBean(list.size(),list);
            return pageBean;
        }
    }

    @Override
    public void delete(Integer id) {
        Student student=articleMapper.selectByArticleId(id);
        articleMapper.updateArticleNum(student);
        articleMapper.deleteById(id);
    }

    @Override
    public void Insert(ArticleInsert article) {
        articleMapper.Insert(article);
    }

    @Override
    public void Update(Integer id, ArticleInsert article) {
        articleMapper.Update(id,article);
    }

    @Override
    public Article Select(Integer id) {
        return articleMapper.Select(id);
    }

}
